package org.ruixue.controller;

import cn.rkylin.oms.common.dto.DBLog;
import com.alibaba.fastjson.JSON;
import org.ruixue.response.BaseResponse;
import org.ruixue.service.StockService;
import org.ruixue.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
@RequestMapping("/stock")
public class StockController {

    @Autowired
    LogUtil logUtil;

    @Autowired
    StockService stockService;

    /**
     * http://localhost:40005/microservice-yiyouyayin/stock/synchStockYiItem?shopId=55ed507f052747a0aa97d56d63f53f99
     * 商品同步库存 按店铺
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/synchStockYiItem", method = RequestMethod.GET)
    private ResponseEntity<BaseResponse> synchStockYiItem(@RequestParam String shopId) {
        BaseResponse response = new BaseResponse();
        try {
            response = stockService.synchStockYiItem(shopId);
            logUtil.info(new DBLog(shopId, "", "synchStockYiItem", JSON.toJSONString(response)));
        } catch (Exception e) {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setSuccess("false");
            logUtil.error(new DBLog(shopId, "", "synchStockYiItem", e.getMessage()), e);
        }
        return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
    }

    /**
     * 商品同步库存 根据商品
     * http://localhost:40005/microservice-yiyouyayin/stock/synchStockYiItemByItem?shopId=55ed507f052747a0aa97d56d63f53f99&ecItemId=6fe842cc9b4148489e79375bb170697e
     * @param shopId
     * @param ecItemId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/synchStockYiItemByItem", method = RequestMethod.GET)
    private ResponseEntity<BaseResponse> synchStockYiItemByItem(@RequestParam String shopId, @RequestParam String ecItemId) {
        BaseResponse response = new BaseResponse();
        try {
            response = stockService.synchStockYiItemByItem(shopId, ecItemId);
            logUtil.info(new DBLog(shopId, "", "synchStockYiItemByItem", JSON.toJSONString(response)));
        } catch (Exception e) {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setSuccess("false");
            logUtil.error(new DBLog(shopId, "", "synchStockYiItemByItem", e.getMessage()), e);
        }
        return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
    }


    /**
     * 商品同步库存 根据sku
     * http://localhost:40005/microservice-yiyouyayin/stock/synchStockJdItemBySku?shopId=55ed507f052747a0aa97d56d63f53f99
     * @param shopId
     * @param ecSkuId
     * @return
     */
    /*@ResponseBody
    @RequestMapping(value = "/synchStockSuningItemBySku", method = RequestMethod.GET)
    private ResponseEntity<BaseResponse> synchStockJdItemBySku(@RequestParam String shopId, @RequestParam String ecSkuId) {
        BaseResponse response = new BaseResponse();
        try {
            response = stockService.synchStockSuningItemBySku(shopId, ecSkuId);
            logUtil.info(new DBLog(shopId, "", "synchStockSuningItemBySku", JSON.toJSONString(response)));
        } catch (Exception e) {
            e.printStackTrace();
            response.setMsg(e.getMessage());
            response.setSuccess("false");
            logUtil.error(new DBLog(shopId, "", "synchStockSuningItemBySku", e.getMessage()), e);
        }
        return new ResponseEntity<BaseResponse>(response, HttpStatus.OK);
    }*/
}
